const Base = require('./base.js');
module.exports = class extends Base {
    indexAction() {
        return this.display();
    }

    /**
     * 分页加载表格数据
     * @returns json字符串
     */
    async listAction() {
        let data = this.get();
        let master_name=data.master_name;
        let local_name=data.local_name;
        let instance_name = data.instance_name;
        let yun_platform = data.yun_platform;
        let init_num = data.init_num;
        let whereMap = {};
        if (!think.isEmpty(master_name)) {
                whereMap .master_name= ['like',`%${master_name}%`];
        }
        if (!think.isEmpty(local_name)) {
            whereMap.local_name = ['like', `%${local_name}%`];
        }
        if (!think.isEmpty(instance_name)) {
            whereMap.instance_name = ['like', `%${instance_name}%`];
        }
        if (!think.isEmpty(yun_platform)) {
            whereMap.yun_platform = ['like', `%${yun_platform}%`];
        }
        if (!think.isEmpty(init_num)) {
            whereMap.init_num = ['like', `%${init_num}%`];
        }

        let model = this.model('master');
        let result = await model.where(whereMap).page(this.get('page'), this.get('limit')).countSelect();
        return this.jsonPage(result);
    }

    /**
     * 获取角色信息
     * @returns json字符串
     */
    async getRoleAction() {
        let model = this.model('role');
        let data = await model.field('permission_name,id').select();
        return this.json(data);
    }

    /**
     * 修改用户信息
     * @returns 返回code
     */
    async updateAction() {
        let model = this.model('user');
        if (this.isPost) {
            let data = this.post();
            let id = this.post('userid');
            let number = await model.where({id: id}).update(data);
            return this.json(number);
        } else {
            let user_id = this.get('id');
            let data = await model.where({id: user_id}).find();
            this.assign('user', data);
            return this.display();
        }
    }


    /**
     * 修改状态
     * @returns  返回code
     */
    async enabledAction() {
        let data = this.post();
        let model = this.model('user');
        let success = await model.where({id: data.userId}).update({enabled: data.enabled});
        return this.json(success);
    }

    /**
     * 添加用户
     * @returns json
     */
    async addAction() {
        let model = this.model('user');
        let userRole = this.model('user_role');
        let userinfo = await this.assign('userinfo');
        if (this.isPost) {
            let data = this.post();
            data.password = think.md5(data.password);
            data.created_by_id = userinfo.id;
            data.updated_by_id = userinfo.id;
            data.enabled = 1;
            try {
                let success = await model.add(data);
                if (success > 0) {
                    try {
                        let roleId = data.userRole;
                        let uId = success;
                        await userRole.add({user_id: uId, role_id: roleId});
                        return this.json('1');
                    } catch (e) {
                        return this.json(2);
                    }
                }
            } catch (e) {
                return this.json('0');
            }
        } else {
            return this.display();
        }
    }

};


